package com.gljx.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.RemittanceBkf;
import com.gljx.web.Entity.RemittanceBkfEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.RemittanceBkfEntryMapper;
import com.gljx.web.dao.RemittanceBkfMapper;
import com.gljx.web.service.RemittanceBkfService;

@Transactional
@Service
public class RemittanceBkfServiceImpl extends CommonDao implements RemittanceBkfService{

	@Autowired
	private RemittanceBkfMapper remittanceBkfMapper;
	
	@Autowired
	private RemittanceBkfEntryMapper remittanceBkfEntryMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.RemittanceBkfMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.RemittanceBkfMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询转付补考费
	 */
	@Override
	public List<RemittanceBkfEntry> queryRemittanceBkfEntry(String fparentId, String cfidcardno, String studentName) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_fparentid", fparentId);
		paramMap.put("v_cfidcardno", cfidcardno);
		paramMap.put("v_studentName", studentName);
		paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.RemittanceBkfEntryMapper.selectRemittanceBkfEntry", paramMap, null);
		@SuppressWarnings("unchecked")
		List<RemittanceBkfEntry> remittanceBkfEntrys = (List<RemittanceBkfEntry>) paramMap.get("v_cursor");
		return remittanceBkfEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editRemittanceBkf(RemittanceBkf remittanceBkf, String[] fseq, String[] cfstudentid,
			String[] cfstudentno, String[] cfidcardno, String[] cfsex, String[] cfslcx, String[] cfamount,
			String[] cffukuang, String[] cfguazhang, String[] cffpsj, String[] cffphm, String[] cfdesc, User user,
			String time) {
		// TODO Auto-generated method stub
		if("".equals(remittanceBkf.getFid()) || remittanceBkf.getFid() == null){
			//创建人id
			remittanceBkf.setFcreatorid(user.getFid());
			//利用UUID生成fid
			remittanceBkf.setFid(GetFid.UUIDIntoFid());
			//当前时间
			remittanceBkf.setFcreatetime(time);
			//状态
            remittanceBkf.setCfbillstate(3L);
            //添加
			remittanceBkfMapper.insertSelective(remittanceBkf);
		}else{
			//修改时间时间
			remittanceBkf.setFlastupdatetime(time);
			//修改人id
			remittanceBkf.setFlastupdateuserid(user.getFid());
			//修改
			remittanceBkfMapper.updateByPrimaryKeySelective(remittanceBkf);
			//先删除子类信息
			remittanceBkfEntryMapper.deleteByFparentId(remittanceBkf.getFid());
		}
		//添加子类信息
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				RemittanceBkfEntry remittanceBkfEntry = new RemittanceBkfEntry();
				remittanceBkfEntry.setCfstudentid(cfstudentid[i]);
				remittanceBkfEntry.setCfstudentno(cfstudentno[i]);
				remittanceBkfEntry.setCfidcardno(cfidcardno[i]);
				remittanceBkfEntry.setCfsex(Long.parseLong(cfsex[i]));
				remittanceBkfEntry.setCfslcx(cfslcx[i]);
				if("".equals(cfamount[i])){
					
				}else{
					remittanceBkfEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				if("".equals(cffukuang[i])){
					
				}else{
					remittanceBkfEntry.setCffukuang(BigDecimalFormat.formot(cffukuang[i]));
				}
				if("".equals(cfguazhang[i])){
					
				}else{
					remittanceBkfEntry.setCfguazhang(BigDecimalFormat.formot(cfguazhang[i]));
				}
				remittanceBkfEntry.setCffpsj(cffpsj[i]);
				remittanceBkfEntry.setCffphm(cffphm[i]);
				remittanceBkfEntry.setCfdesc(cfdesc[i]);
				remittanceBkfEntry.setFseq((long)i+1);
				remittanceBkfEntry.setFid(GetFid.UUIDIntoFid());
				remittanceBkfEntry.setFparentid(remittanceBkf.getFid());
				remittanceBkfEntryMapper.insertSelective(remittanceBkfEntry);
			}
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public RemittanceBkf queryRemittanceBkfByFid(String fid) {
		// TODO Auto-generated method stub
		return remittanceBkfMapper.selectRemittanceBkfByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
	public void deleteRemittanceBkfByFid(String fid, String[] deleteId) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				//先删除子类信息
				remittanceBkfEntryMapper.deleteByFparentId(deleteId[i]);
				//在删除父类信息
				remittanceBkfMapper.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			//先删除子类信息
			remittanceBkfEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			remittanceBkfMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 审核，反审核
	 */
	@Override
    public int auditRemittanceBkf(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
		RemittanceBkf remittanceBkf = new RemittanceBkf();
        Axamine axamine = new Axamine();
        int i;
        if("1".equals(type)){
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            remittanceBkf.setCfbillstate(5L);
            if (serverResponse.getStatus() == 1) {
                //fid
                remittanceBkf.setFid(fid);
                //审核人
                remittanceBkf.setFauditorid(fhandlerId);
                //修改人
                remittanceBkf.setFlastupdateuserid(fhandlerId);
                //结束
                i = remittanceBkfMapper.updateByPrimaryKeySelective(remittanceBkf);
                return i;
            } else if (serverResponse.getStatus() == 2) {
                if (!"1".equals(state)) {
                    remittanceBkf.setFid(fid);
                    remittanceBkf.setCfbillstate(3L);
                    remittanceBkfMapper.updateByPrimaryKeySelective(remittanceBkf);
                }
                //推进
                return i = 2;
            } else {
                //错误
                return i = 0;
            }
        }else{
            axamine.againstAuditProcess(fnumber, message);
            remittanceBkf.setCfbillstate(3L);
            remittanceBkf.setFid(fid);
            return remittanceBkfMapper.updateByPrimaryKeySelective(remittanceBkf);
        }
    }

    @Override
    public int mofidyRemittanceBkf(RemittanceBkf remittanceBkf) {
        return remittanceBkfMapper.updateByPrimaryKeySelective(remittanceBkf);
    }

    @Override
    public List<RemittanceBkfEntry> selectRemittanceCkf(String fcompanyid, String studentName, String cfidcardno, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("cfidcardno", cfidcardno);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return remittanceBkfEntryMapper.selectRemittanceCkf(map);
    }
}
